﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Entity;
using ClassLibrary.Data;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblProductsDAO
    {
        DBService _db = null;
        public TblProductsDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }
        public bool InsUpdProducts(TblProducts Product, int mode)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblProducts_InsUpd",
                    new string[] { "@ProductID", "@ProductName", "@ProductCode", "@IDmodel", "@IDPhoneBrand", "@ProductType", "@ProductDescription",
                                   "@Cost", "@RRP", "@QuantityOnHand","@Type", "@mode" },
                    new object[] { Product.ProductID, Product.ProductName, Product.ProductCode, Product.IDModel, Product.IDPhoneBrand, Product.ProductType, Product.ProductDescription,
                                   Product.Cost, Product.RRP, Product.QuantityOnHand, Product.Type, mode},
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable GetProductsList(int Type)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_GetProducts", 
                    new string[]{"@idType"},
                    new object[]{Type}, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // get product By IDModel, IDBrand
      
        public DataTable GetProductsList_ByID(int Type, int IDModel, int IDBrand, string ProductType, int mode)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_GetProducts_ByID", 
                    new string[]{"@idType", "@IDModel", "@IDMenuFac", "@ProductType", "@mode" },
                    new object[]{Type, IDModel, IDBrand, ProductType, mode}, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
         }

        public DataTable GetProductById(int ProductID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblProducts where ProductID = @ProductID",
                    new string[] { "@ProductID" },
                    new object[] { ProductID},
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public bool DeleteProduct(int ProductID)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("delete TblProducts where ProductID = @ProductID",
                    new string[] { "@ProductID" },
                    new object[] {ProductID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable GetImgItems(int ProductID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblProductImage where ProductID = @ProductID",
                    new string[] { "@ProductID" },
                    new object[] { ProductID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public bool InsImageItem(TblProductImage productImg, int mode)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblItemImage_Ins",
                    new string[] { "@p_ProductID", "@p_ImageURL" },
                    new object[] { productImg.ProductID, productImg.ImageURL },
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public TblProductImage GetItemImageByID(int ItemID, int ID)
        {

            TblProductImage image = new TblProductImage();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("sp_TblItemImage_Lst",
                   new string[] { "@p_ProductID", "@Id" },
                   new object[] { ItemID, ID },
                  CommandType.StoredProcedure);

                foreach (DataRow dr in dt.Rows)
                {
                    image.ID = Convert.ToInt32(dr["ID"].ToString());
                    image.ImageURL = dr["ImageURL"].ToString();
                    image.IsDefault = Convert.ToBoolean(dr["IsDefault"].ToString());
                    image.ProductID = Convert.ToInt32(dr["ProductID"].ToString());

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return image;
        }

        public bool DeleteImgItem(TblProductImage imgItes)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblItemImage_Del",
                    new string[] { "@p_ID" },
                    new object[] { imgItes.ID },
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable GetPart_ByID(int ProductID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("GetPart",
                                         new string[] { "@ProductID" },
                                         new object[] {ProductID}, CommandType.StoredProcedure);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return dt;
        }

        public DataTable GetPartSearch(string ProductName, int type)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_GetPart_Search",
                    new string[] { "@ProductName", "@Type" },
                    new object[] { ProductName, type }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetProductByIDProduct_Table(int ProductID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblProducts where TblProducts.ProductID = @ProductID",
                    new string[] { "@ProductID" },
                    new object[] { ProductID }
                    , CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // Lấy sản phẩm theo ID
        public TblProducts GetProductByIDProduct(int ProductID)
        {
            TblProducts item = new TblProducts();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("select * from TblProducts where TblProducts.ProductID = @ProductID",
                    new string[] { "@ProductID" },
                    new object[] { ProductID }
                    , CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    item.ProductID = ProductID;
                    item.ProductName = dr["ProductName"].ToString();
                    item.ProductCode = dr["productCode"].ToString();
                    item.IDModel = Convert.ToInt32(dr["IDmodel"].ToString());
                    item.IDPhoneBrand = Convert.ToInt32(dr["IDPhoneBrand"].ToString());
                    item.ProductType = dr["ProductType"].ToString();
                    item.ProductDescription = dr["ProductDescription"].ToString();
                    item.Cost = decimal.Parse(dr["Cost"].ToString());
                    item.RRP = decimal.Parse(dr["RRP"].ToString());
                    item.QuantityOnHand = Convert.ToInt32(dr["QuantityOnHand"].ToString());
                    item.Type = Convert.ToInt32(dr["Type"].ToString());                
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return item;
        }




        // get all product
        public DataTable GetAllProduct()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblProducts", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;

            }
            return dt;
        }
    }
}
